#!/usr/bin/env expect
############################################################################
# Purpose: Test of Slurm functionality
#          Test of running different executables with different arguments
#          for each task (--multi-prog option with poe). This is a variant
#          of test1.54 for POE systems
#
# Output:  "TEST: #.#" followed by "SUCCESS" if test was successful, OR
#          "FAILURE: ..." otherwise with an explanation of the failure, OR
#          anything else indicates a failure mode that must be investigated.
############################################################################
# Copyright (C) 2006 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
# Written by Morris Jette <jette1@llnl.gov>
# CODE-OCEC-09-009. All rights reserved.
#
# This file is part of Slurm, a resource management program.
# For details, see <https://slurm.schedmd.com/>.
# Please also read the included file: DISCLAIMER.
#
# Slurm is free software; you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 2 of the License, or (at your option)
# any later version.
#
# Slurm is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along
# with Slurm; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA.
############################################################################
source ./globals

set test_id     "11.8"
set file_in     "test$test_id.input"
set exit_code   0

print_header $test_id

if {![test_launch_poe]} {
	send_user "\nWARNING: This test is incompatible with non-POE systems\n"
	exit 0
}

#
# Delete left-over input script
# Build input script file
#
exec $bin_rm -f $file_in
set file [open $file_in "w"]
puts $file "# multi-program configuration file
1   /bin/echo task:1:offset:0
2   /bin/echo task:2:offset:1
0   echo task:0:offset:0
3   echo task:3:offset:1
"
close $file
exec $bin_chmod 700 $file_in

#
# Submit a slurm job that will execute different programs and arguments by task number
#
set matches 0
set timeout $max_job_delay
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog ./$file_in]
expect {
	-re "($number): *task:($number):offset:($number)" {
		set label       $expect_out(1,string)
		set task_id     $expect_out(2,string)
		set task_offset $expect_out(3,string)
		if {$label == 0 && $task_id == 0 && $task_offset == 0} {
			incr matches
		}
		if {$label == 1 && $task_id == 1 && $task_offset == 0} {
			incr matches
		}
		if {$label == 2 && $task_id == 2 && $task_offset == 1} {
			incr matches
		}
		if {$label == 3 && $task_id == 3 && $task_offset == 1} {
			incr matches
		}
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 4} {
	send_user "\nFAILURE: Did not get expected multi-program output\n"
	set exit_code 1
}
if {$exit_code != 0} {
	exit $exit_code
} else {
	send_user "\nSo far, so good\n\n"
}

#
# Submit a slurm job that will execute different executables and check debug info
#
# Timeout is max_job_delay (to spawn task) +
#       60 (job time limit) +
#       60 (slurmctld time limit check poll interval) +
#       KillWait
#
set timeout [expr $max_job_delay + 60 + 60 + 60]

exec $bin_rm -f $file_in
set file [open $file_in "w"]
puts $file "# multi-program configuration file
1,3   true
0,2   date
"
close $file
exec $bin_chmod 700 $file_in

set job_id    -
set matches   0
set srun_pid [spawn $srun -N1 -n4 --overcommit -l -t1 --multi-prog -v ./$file_in]
expect {
	-re "launching ($number)" {
		set job_id $expect_out(1,string)
		exp_continue
	}
	-re " 0 2:.*($number):($number):($number)" {
		incr matches
		exp_continue
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: did not generate one date.\n"
	set exit_code 1
}
if {$job_id == 0} {
	send_user "\nFAILURE: failed to get job id\n"
	set exit_code 1
} else {
	cancel_job $job_id
}

if {$exit_code != 0} {
	exit $exit_code
} else {
	send_user "\nSo far, so good\n\n"
}

#
# Submit a slurm job that asks for more tasks than specified in our
# configuration file
#
set matches   0
set srun_pid [spawn $srun -N1 -n5 --overcommit -l -t1 --multi-prog  ./$file_in]
expect {
	-re "Configuration file .*invalid" {
		send_user "\nNo worries. This error is expected\n"
		incr matches
	}
	timeout {
		send_user "\nFAILURE: srun not responding\n"
		slow_kill $srun_pid
		set exit_code 1
	}
	eof {
		wait
	}
}
if {$matches != 1} {
	send_user "\nFAILURE: did not note lack of a executable for task 5.\n"
	set exit_code 1
}

if {$exit_code == 0} {
	exec $bin_rm -f $file_in
	send_user "\nSUCCESS\n"
}
exit $exit_code
